package com.xiaobug.demo.loadsir;

import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;

import com.ljb.commlibrary.utils.ThreadUtils;
import com.xiaobug.baselibrary.base.BaseActivity;
import com.xiaobug.baselibrary.ui.loadlayout.LoadStateUtils;
import com.xiaobug.demo.R;
import com.xiaobug.demo.databinding.LoadsirActivityTestBinding;

/**
 * Created by ljb on 2018/5/9.
 */

public class LoadsirActivity extends BaseActivity<LoadsirActivityTestBinding> {


    private LoadStateUtils stateUtils;

    public class Presenter {
        public void onClick1(View view) {
            reset();

            stateUtils = new LoadStateUtils();
            stateUtils.setLoadStateListener(mBinding.fl);

            ThreadUtils.runInThread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    stateUtils.setState(LoadStateUtils.STATE_SUCCESS);
                }
            });
        }

        public void onClick2(View view) {
            reset();

            stateUtils = new LoadStateUtils();
            stateUtils.setLoadStateListener(mBinding.fl);

            ThreadUtils.runInThread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    stateUtils.setState(LoadStateUtils.STATE_ERROR);
                }
            });
        }

        public void onClick3(View view) {
            reset();

            stateUtils = new LoadStateUtils();
            stateUtils.setLoadStateListener(mBinding.fl, new LoadStateUtils.OnReloadListener() {
                @Override
                public void onReload(View view) {
                    ThreadUtils.runInThread(new Runnable() {
                        @Override
                        public void run() {
                            SystemClock.sleep(3000);
                            stateUtils.setState(LoadStateUtils.STATE_SUCCESS);
                        }
                    });
                }
            });

            ThreadUtils.runInThread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    stateUtils.setState(LoadStateUtils.STATE_EMPTY);
                }
            });
        }

        private void reset() {
            if (stateUtils != null)
                stateUtils.setState(LoadStateUtils.STATE_SUCCESS);
        }
    }

    @Override
    protected int getRootID() {
        return R.layout.loadsir_activity_test;
    }

    @Override
    protected void initUI(Bundle savedInstanceState, LoadsirActivityTestBinding binding) {
        binding.setP(new Presenter());

    }

    @Override
    protected void initData(Bundle savedInstanceState, LoadsirActivityTestBinding binding) {

    }
}
